package com.lagou;

import com.lagou.service.bean.HelloServiceBean;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Application {

    public static void main(String[] args) throws InterruptedException, IOException {
        System.out.println("-------------");
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        // 获取消费者组件
        HelloServiceBean service = context.getBean(HelloServiceBean.class);
        ExecutorService executorService = Executors.newFixedThreadPool(200);
        while (true) {
            long start = System.currentTimeMillis();
            for (int i = 0; i < 2010; i++) {
                executorService.submit(() -> {
                    String hello = service.methodA("world");
                    System.out.println("result:" + hello);
                });
                executorService.submit(() -> {
                    String hello = service.methodB("world");
                    System.out.println("result:" + hello);
                });
                executorService.submit(() -> {
                    String hello = service.methodC("world");
                    System.out.println("result:" + hello);
                });
            }
            long end = System.currentTimeMillis();
            long value = 60000 - (end - start);
            if (value > 0) {
                Thread.sleep(value);
            }
        }
    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan(basePackages = "com.lagou")
    @EnableDubbo
    @EnableScheduling
    static class ConsumerConfiguration {

    }

}